/**
 * 六边形网格
 */

using GameBaseFramework.Base;
using GameBaseFramework.Extension;
using GameLogic;
using streaming;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

namespace GameGraphics.HomeWorld
{
    public class HexGrid : MonoBehaviour
    {
        /// <summary>
        /// 画布
        /// </summary>
        public Canvas Canvas;
        /// <summary>
        /// Mesh
        /// </summary>
        public HexMesh HexMesh;
        /// <summary>
        /// 单位标签预制体
        /// </summary>
        public Text CellLabelPrefab;
        /// <summary>
        /// 空闲格子颜色
        /// </summary>
        public Color IdleGridColor = Color.clear;
        /// <summary>
        /// 鼠标移动上去且可以选中的格子颜色
        /// </summary>
        public Color Hovered_Selectable_Color = Color.blue;
        /// <summary>
        /// 鼠标移动上去且不可以选中的格子颜色
        /// </summary>
        public Color Hovered_UnSelectable_Color = Color.red;
        /// <summary>
        /// 鼠标移动上去且不可以选中的格子颜色
        /// </summary>
        public Color Selected_Color = Color.yellow;
        /// <summary>
        /// 鼠标移动上去的格子边缘颜色
        /// </summary>
        public Color BridgeHoveredColor = Color.yellow;
        /// <summary>
        /// 被选中的格子边缘颜色
        /// </summary>
        public Color BridgeSelectedColor = Color.white;
        /// <summary>
        /// 空闲状态的格子边缘颜色
        /// </summary>
        public Color BridgeIdleColor = Color.black;
        /// <summary>
        /// 对应格子状态颜色
        /// </summary>
        private List<Color> StateColors = new List<Color>
        {
            Color.green, Color.red, Color.red, Color.red, Color.red, Color.green
        };
        /// <summary>
        /// 宽
        /// </summary>
        public int Width = 10;
        /// <summary>
        /// 高
        /// </summary>
        public int Height = 10;
        /// 单位数据
        /// </summary>
        public List<HexCell> Cells { get; set; }


        /// <summary>
        /// 初始化
        /// </summary>
        public void Awake()
        {
            InitStateColor();
            if (!Cells.IsNullOrEmpty())
                Cells.Clear();
            else
                Cells = new();
            for (int z = 0, i = 0; z < Height; z++)
            {
                for (int x = 0; x < Width; x++)
                {
                    CreateCell(x, z, i++);
                }
            }
        }

        /// <summary>
        /// 初始化状态颜色数组
        /// </summary>
        private void InitStateColor()
        {
            StateColors.Clear();
            StateColors.Add(IdleGridColor);
            StateColors.Add(Hovered_Selectable_Color);
            StateColors.Add(Hovered_UnSelectable_Color);
            StateColors.Add(Selected_Color);
        }

        /// <summary>
        /// 创建格子
        /// </summary>
        private void CreateCell(int x, int z, int i)
        {
            var position = HexMetrics.GetCenterPosition(x, z);
            var cell= new HexCell();
            cell.Indexs = new Vector2Int(x, z);
            cell.LocalPosition = position;
            cell.Color = new Color(0, 0, 0, 0);
            cell.State = CellState.Idle;
            if (Canvas != null)
            {
                var label = Instantiate<Text>(CellLabelPrefab);
                label.rectTransform.SetParent(Canvas.transform, false);
                label.rectTransform.anchoredPosition = new Vector2(position.x + 0.5f , position.z + 0.5f);
                var r = cell.Indexs.x - cell.Indexs.y / 2;
                var s = cell.Indexs.y;
                var q = -r - s;
                //label.text = r.ToString() + "\n" + q.ToString() + "\n" + s.ToString();
                label.text = $"{cell.Indexs.x},{cell.Indexs.y}";
            }
            Cells.Add(cell);
        }

        /// <summary>
        /// 显示网格
        /// </summary>
        public void ShowMesh()
        {
            Repaint();
            HexMesh.gameObject.SetActive(true);
        }

        /// <summary>
        /// 隐藏网格
        /// </summary>
        public void HideMesh()
        {
            HexMesh.gameObject.SetActive(false);
        }


        /// <summary>
        /// 返回Cell
        /// </summary>
        public HexCell GetCell(Vector2Int indexs)
        {
            return GetCell(indexs.x, indexs.y);
        }

        /// <summary>
        /// 返回Cell
        /// </summary>
        private HexCell GetCell(int x, int z)
        {
            if (x >= 0 && x < Width && z >= 0 && z < Height)
            {
                return Cells[x + z * Width];
            }
            return null;
        }

        /// <summary>
        /// 重新绘制
        /// </summary>
        public void Repaint()
        {
            for (var i = 0; i < Cells.Count; i++)
            {
                var cell = Cells[i];
                cell.Color = StateColors[(int)cell.State];
            }
            HexMesh.Triangulate(Cells);
        }

        /// <summary>
        /// 重画
        /// </summary>
        public void Repaint(List<Vector2Int> useGrids, List<Vector2Int> occupyGrids, List<Vector2Int> unavailableGrids, List<Vector2Int> CanMergeGrids = null)
        {

        }

        /// <summary>
        /// 绘制不可建造格子
        /// </summary>
        public void RepaintGrids(List<Vector2Int> grids, Color color)
        {
            for (var i = 0; i < Cells.Count; i++)
            {
                var cell = Cells[i];
                var isFind = grids != null && grids.FindIndex((value) => value == cell.Indexs) != -1;
                if (isFind)
                {
                    cell.Color = color;
                }
                else
                {
                    cell.Color = IdleGridColor;
                }
            }
            HexMesh.Triangulate(Cells);
        }

        /// <summary>
        /// 获取邻居
        /// </summary>
        public HexCell GetNeighbor(HexCell cell, int offsetR, int offsetS)
        {
            int cellR = cell.Indexs.x - cell.Indexs.y / 2;
            int cellS = cell.Indexs.y;
            int r = cellR + offsetR;
            int s = cellS + offsetS;
            int x = r + s / 2;
            int z = s;
            return GetCell(x, z);
        }
    }
}
